package com.boc.crm.base.util;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;


public class RedisHelper {
	
	private final static Logger logger = LoggerFactory.getLogger(RedisHelper.class);
     
	public static Set<String> keys(String keys){
		Jedis jedis = null;
		try{
			jedis = RedisProvider.getJedis();
			Set<String> set = jedis.keys(keys);
			return set;
		} catch(Exception e) {
			logger.error("keys方法报错" , e);
			throw new RuntimeException("expire方法报错。");
		} finally {
			if(jedis!=null){
				RedisProvider.returnJedis(jedis);
			}
		}
	}
	
	/** 设置redis对应的key和超时时间 */
	public static Long expire(String key, int seconds) {
		Jedis jedis = null;
		try{
			jedis = RedisProvider.getJedis();
			Long rtn = jedis.expire(key, seconds);
			logger.info("set data expire of rcs redis: key=" + key + "  , seconds=" + seconds + " , rtn=" + rtn);
			return rtn;
		} catch(Exception e) {
			logger.error("expire方法报错：key=" + key, e);
			throw new RuntimeException("expire方法报错。");
		} finally {
			if(jedis!=null){
				RedisProvider.returnJedis(jedis);
			}
		}
	}
	
	/** 移除redis对应的key超时时间 */
	public static Long persist(String key) {
		Jedis jedis = null;
		try{
			jedis = RedisProvider.getJedis();
			Long rtn = jedis.persist(key);
			logger.info("set data persist of rcs redis: key=" + key + "  , rtn=" + rtn);
			return rtn;
		} catch(Exception e) {
			logger.error("persist方法报错：key=" + key, e);
			throw new RuntimeException("persist方法报错。");
		} finally {
			if(jedis!=null){
				RedisProvider.returnJedis(jedis);
			}
		}
	}
	
	
	 public static String set(String key, String value){
		  Jedis jedis = null;
		  try{
		    jedis = RedisProvider.getJedis();
		    String rtn = jedis.set(key, value);
		    return rtn;
		  } catch(Exception e) {
			logger.error("set 方法报错：key=" + key, e);
			throw new RuntimeException("set 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }
	 
	 public static Long setWithTimeOut(String key, String value,int seconds){
		  Jedis jedis = null;
		  try{
		    jedis = RedisProvider.getJedis();
		    jedis.set(key, value);
		    Long rtn=jedis.expire(key, seconds);
		    return rtn;
		  } catch(Exception e) {
			logger.error("setWithTimeOut 方法报错：key=" + key, e);
			throw new RuntimeException("setWithTimeOut 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }
	 
	  public static String flushdb(){
		Jedis jedis = null;
		try{
		    jedis = RedisProvider.getJedis();
		    String rtn = jedis.flushDB();
		    return rtn;
		  } catch(Exception e) {
			logger.error("flushdb 方法报错", e);
			throw new RuntimeException("flushdb 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static Long del(String keys){
		Jedis jedis = null;
		try{
		    jedis = RedisProvider.getJedis();
		    Long rtn = jedis.del(keys);
		    return rtn;
		  } catch(Exception e) {
			logger.error("del 方法报错  ", e);
			throw new RuntimeException("del 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static String get(String key){
		 Jedis jedis = null;
		 try{
		    jedis = RedisProvider.getJedis();
		    String rtn = jedis.get(key);
		    return rtn;
		 } catch(Exception e) {
			logger.error("get 方法报错  key="+key, e);
			throw new RuntimeException("get 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  	}

	  public static String mset(String... keysvalues){
		 Jedis jedis = null;
		 try{
		    jedis = RedisProvider.getJedis();
		    String rtn = jedis.mset(keysvalues);
		    return rtn;
		 } catch(Exception e) {
			logger.error("mset 方法报错  ", e);
			throw new RuntimeException("mset 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static List<String> mget(String... keys){
		 Jedis jedis = null;
		 try{
		    jedis = RedisProvider.getJedis();
		    List<String> rtn = jedis.mget(keys);
		    return rtn;
		 } catch(Exception e) {
			logger.error("mget 方法报错 ", e);
			throw new RuntimeException("mget 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static Long sadd(String key, String... members){
		  Jedis jedis = null;
		  try{
		    jedis = RedisProvider.getJedis();
		    Long rtn = jedis.sadd(key, members);
		    return rtn;
		  } catch(Exception e) {
			logger.error("sadd 方法报错  key="+key, e);
			throw new RuntimeException("sadd 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static Long srem(String key, String... members){
		  Jedis jedis = null;
		  try{
		    jedis = RedisProvider.getJedis();
		    Long rtn = jedis.srem(key, members);
		    return rtn;
		  } catch(Exception e) {
			logger.error("srem 方法报错  key="+key, e);
			throw new RuntimeException("srem 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static Long scard(String key){
		  Jedis jedis = null;
		  try{
		    jedis = RedisProvider.getJedis();
		    Long rtn = jedis.scard(key);
		    return rtn;
		  } catch(Exception e) {
			logger.error("scard 方法报错   key="+key, e);
			throw new RuntimeException("scard 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static Boolean sismember(String key, String member){
		  Jedis jedis = null;
		  try{
		    jedis = RedisProvider.getJedis();
		    Boolean rtn = jedis.sismember(key, member);
		    return rtn;
		  } catch(Exception e) {
			logger.error("sismember 方法报错    key="+key, e);
			throw new RuntimeException("sismember 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static Set<String> smembers(String key){
		  Jedis jedis = null;
		  try{
		    jedis = RedisProvider.getJedis();
		    Set<String> rtn = jedis.smembers(key);
		    return rtn;
		  } catch(Exception e) {
			logger.error("smembers 方法报错   key="+key, e);
			throw new RuntimeException("smembers 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static Set<String> sunion(String... keys){
		  Jedis jedis = null;
		  try{
		    jedis = RedisProvider.getJedis();
		    Set<String> rtn = jedis.sunion(keys);
		    return rtn;
		  } catch(Exception e) {
			logger.error("sunion 方法报错", e);
			throw new RuntimeException("sunion 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static Set<String> sinter(String... keys){
		  Jedis jedis = null;
		  try{
		    jedis = RedisProvider.getJedis();
		    Set<String> rtn = jedis.sinter(keys);
		    return rtn;
		  } catch(Exception e) {
			logger.error("sinter 方法报错", e);
			throw new RuntimeException("sinter 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static Long hset(String key, String field, String value){
	  	  Jedis jedis = null;
		  try{
			jedis = RedisProvider.getJedis();
		    Long rtn = jedis.hset(key, field, value);
		    return rtn;
		  } catch(Exception e) {
			logger.error("hset 方法报错  key="+key, e);
			throw new RuntimeException("hset 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static String hget(String key, String field){
	  	  Jedis jedis = null;
		  try{
			jedis = RedisProvider.getJedis();
		    String rtn = jedis.hget(key, field);
		    return rtn;
		  } catch(Exception e) {
			logger.error("hget 方法报错  key="+key, e);
			throw new RuntimeException("hget 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static String hmset(String key, Map<String, String> hash){
	  	  Jedis jedis = null;
		  try{
			jedis = RedisProvider.getJedis();
		    String rtn = jedis.hmset(key, hash);
		    return rtn;
		  } catch(Exception e) {
			logger.error("hmset 方法报错  key="+key, e);
			throw new RuntimeException("hmset 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static List<String> hmget(String key, String... field){
	  	  Jedis jedis = null;
		  try{
			jedis = RedisProvider.getJedis();
		    List<String> rtn = jedis.hmget(key, field);
		    return rtn;
		  } catch(Exception e) {
			logger.error("hmget 方法报错   key="+key, e);
			throw new RuntimeException("hmget 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static Map<String, String> hgetall(String key){
	  	  Jedis jedis = null;
		  try{
			jedis = RedisProvider.getJedis();
		    Map<String,String> rtn = jedis.hgetAll(key);
		    return rtn;
		  } catch(Exception e) {
			logger.error("hgetall 方法报错  key="+key, e);
			throw new RuntimeException("hgetall 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }

	  public static Long hdel(String key, String... field){
	  	  Jedis jedis = null;
		  try{
			jedis = RedisProvider.getJedis();
		    Long rtn = jedis.hdel(key, field);
		    return rtn;
		  } catch(Exception e) {
			logger.error("hdel 方法报错  key="+key, e);
			throw new RuntimeException("hdel 方法报错。");
		  } finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
	  }
	  
		public static Long incr(String key){
			Jedis jedis = null;
			try{
				jedis = RedisProvider.getJedis();
				return jedis.incr(key);
			} catch(Exception e) {
				logger.error("key方法报错" , e);
				throw new RuntimeException("incr方法报错。");
			} finally {
				if(jedis!=null){
					RedisProvider.returnJedis(jedis);
				}
			}
		}
	  
}
